package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.client.Spend_listClient;
import com.example.common.Result;
import com.example.domain.Books;
import com.example.domain.Borrow;
import com.example.domain.Information;
import com.example.domain.Users;
import com.example.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/infor")
public class InformationController {

    @Autowired
    private InformationService informationService;
    @Autowired
    private Spend_listClient spend_listClient;

/*
    上传头像文件的时候，用axios发送post请求，携带文件参数之后，
    不知道怎么携带userId参数，所以无法上传完就根据用户更新信息，所以就先把photo存起来，
    等到发送了更新头像的请求后再利用这个photo进行更新
 */

    private String photo = null;

    /*
    根据userId获取个人详细信息
     */
    @GetMapping("/selectInformationById/{userId}")
    public Result selectInformationById(@PathVariable Long userId){
        Information information = informationService.selectInformationById(userId);
        return Result.success(information);
    }

    /*
    更新个人信息
     */
    @GetMapping("/updateInfor")
    public Result updateInfor(@RequestParam String name, @RequestParam Integer sexNum, @RequestParam Date birth, @RequestParam Long id){
        System.out.println(photo);
        informationService.updateInfor(name, sexNum, birth, id);
        return Result.success("修改成功");
    }

    /*
    充值，跟新information的balance，并且添加充值记录在spend_list表里
     */
    @GetMapping("/recharge/{czMoney}/{id}/{uId}")
    public Result recharge(@PathVariable Integer czMoney,@PathVariable Long id, @PathVariable Long uId){
        informationService.recharge(czMoney,id);
        spend_listClient.rechargeRecord(czMoney,uId);
//        spend_listService.rechargeRecord(czMoney,uId);
        return Result.success("充值成功");
    }

    /*
    因为买书更新information的balance，并且根据余额是否充足来判断是否可以购买，可以的话就再添加购买记录在spend_list表里
     */
    @GetMapping("/updateBalance")
    public void updateBalance(@RequestParam Double bookPrice, @RequestParam Long userId){
       informationService.updateBalance(bookPrice, userId);
    }

    /*
    查看买书yue是否充足
     */
    @GetMapping("/selectBalance")
    public boolean selectBalance(@RequestParam Double bookPrice,@RequestParam Long userId){
        return informationService.seelctBalance(bookPrice, userId);
    }

    @PostMapping("/addInformation")
    public void addInfor(@RequestBody Users user){
        informationService.addInfor(user);
    }

//    上传图片
    @PostMapping("/uploadImg")
    public Result uploadFile(@RequestParam("file") MultipartFile file){
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = "E:\\userPhoto\\"; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 利用UUID生成唯一新文件名

        File dest = new File(filePath + fileName);
//        防止创建文件的时候，避免这个文件所在的文件夹不存在，然后抛出异常
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
//            将图片保存到磁盘
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String filename = "/static/" + fileName;
        photo ="http://localhost:8080/static/"+fileName;
//        System.out.println(photo);
        return Result.success("上传图片成功");
    }

    //上传完头像更新头像
    @GetMapping("/updatePhoto/{userId}")
    public Result updatePhoto(@PathVariable Long userId){
        informationService.updatePhoto(photo,userId);
        return Result.success("上传成功");
    }

    /*
   分页查询
    */
    @GetMapping("/selectUsersByPage")
    public Result selectUsersByPage(@RequestParam(defaultValue = "") String name,
                                    @RequestParam Integer current, @RequestParam Integer size){
        Page<Information> users = informationService.selectUsersByPage(name,current, size);
        return Result.success(users);
    }

    /*
    扣除积分
     */
    @PostMapping("/downPoints")
    public Integer downPoints(@RequestBody Borrow borrow){
        return informationService.downPoints(borrow);
    }

    /*
    增加积分
     */
    @PostMapping("/upPoints")
    public void upPoints(@RequestBody Borrow borrow){
        informationService.upPoints(borrow);
    }

    /*
    借阅之前查看积分
     */
    @GetMapping("/selectPoints/{userId}")
    public Result selectPoints(@PathVariable Long userId){
        boolean b = informationService.selectPoints(userId);
        if(b){
            return Result.success();
        }
        return Result.warning("因多次未及时还书，积分不足，列入失信名单，不可借阅！");
    }
}
